import { createSlice } from '@reduxjs/toolkit'
import { Toast } from 'antd-mobile'

export const user = createSlice({
    name: 'user',
    initialState: {
        userpope: [
            {
                id: 1,
                name: '瑶瑶',
                code: '2334211111112',
                phone: '12098764326',
                state:false
            },
            {
                id: 2,
                name: '涛涛',
                code: '9999999032',
                phone: '12413254326',
                 state:false
            },
            {
                id: 3,
                name: '小瑶',
                code: '888888789032',
                phone: '12413254326',
                 state:false
            },
            {
                id: 4,
                name: '小涛',
                code: '2666666789032',
                phone: '12413254326',
                 state:false
            },
        ]
    },
    reducers: {
        delpope(state, { payload }) {
            state.userpope = state.userpope.filter((item => item.id != payload.id))
             Toast.show({
                    icon:'success',
                    content:'删除成功'

                })
        },
        addpope(state,{payload}){
            const item=state.userpope.find((item)=>item.id==payload.id)
            if(!item){
                state.userpope.push(payload)
                Toast.show({
                    icon:'success',
                    content:'新增成功'

                })
            }
            else{
                  Toast.show({
                    icon:'success',
                    content:'已有此人'

                })
            }
        },
        deitpope(state,{payload}){
             const item=state.userpope.find((item)=>item.id==payload.id)
             Object.assign(payload,item)
             Toast.show({
                    icon:'success',
                    content:'编辑成功'

                })
        }
    }
})
// 每个 case reducer 函数会生成对应的 Action creators
export const {delpope,addpope,deitpope } = user.actions

export default user.reducer